package Friends

import scala.io.Source

/**
 * @DATE 2021/12/29/20:02
 * @Author MDK
 * @Version 2021.2.2
 * */
object C01_Friends {
  def main(args: Array[String]): Unit = {
    val lines = Source.fromFile("data/friends.txt").getLines()
//    lines.foreach(println)
    //将数据封装在Map集合中
//    Map[String,Array[String]] 考虑下用List的情况 Map[String,List[String]]
    val tuple: Array[(String, Array[String])] = lines.map(line => {
      val arr = line.split(":")
      val name = arr(0)
      val fri = arr(1).split(",")
      (name, fri)
    }).toArray
//   显示任意对象的好友信息
//    tuples.foreach(tp=>println((tp._1, tp._2.toList)))

    //任意两个人的好友,两层循环  内外循环
    for(i <- 0 to tuple.length-1){
      val name = tuple(i)._1
      val fri = tuple(i)._2
      for(j <- i+1 to tuple.length-1){
        val name2: String = tuple(j)._1
        val fri2: Array[String] = tuple(j)._2
        val list2 = fri.intersect(fri2).toList
        val res = s"$name 和 $name2 的共同好友是"+ list2
        if(list2.length>0){
          println(res)
        }
      }
    }

  }
}
